import Vue from "vue";
import VueRouter from "vue-router";
import login from "../views/login";
import store from "../store";

Vue.use(VueRouter);

const routes = [
  // {path:'/',redirect:'login'},
  {
    path: "/login",
    component: login,
  },
  {
    path: "/",
    component: () => import("../layout"),
    redirect: "/dashboard",
    children: [
      { path: "dashboard", component: () => import("../views/dashboard") },
      { path: "article", component: () => import("../views/article") },
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  const token = store.state.user.token;
  console.log(store.state.user, '::token::');
  if (to.path === "/login" && token) return next("/"); //如果用户登录过，我们不能重新登录
  if (to.path !== "/login" && !token) return next("/login");
  next();
});

// const originalPush = router.prototype.push;
// router.prototype.push = function push(location, onResolve, onReject) {
//   if (onResolve || onReject)
//     return originalPush.call(this, location, onResolve, onReject);
//   return originalPush.call(this, location).catch((err) => err);
// };

export default router;
